using Microsoft.EntityFrameworkCore;
using StudentManagement.Domain.Entities;

namespace StudentManagement.Infrastructure.Data;

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {
    }

    // 实体集合
    public DbSet<Class> Classes { get; set; }
    public DbSet<Course> Courses { get; set; }
    public DbSet<Permission> Permissions { get; set; }
    public DbSet<Role> Roles { get; set; }
    public DbSet<RolePermission> RolePermissions { get; set; }
    public DbSet<Score> Scores { get; set; }
    public DbSet<Student> Students { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<UserRole> UserRoles { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        // 配置复合主键
        modelBuilder.Entity<RolePermission>()
            .HasKey(rp => new { rp.RoleId, rp.PermissionId });

        modelBuilder.Entity<UserRole>()
            .HasKey(ur => new { ur.UserId, ur.RoleId });

        // 配置关系
        modelBuilder.Entity<Student>()
            .HasOne(s => s.Class)
            .WithMany(c => c.Students)
            .HasForeignKey(s => s.ClassId)
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<Course>()
            .HasOne(c => c.Class)
            .WithMany(cl => cl.Courses)
            .HasForeignKey(c => c.ClassId)
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<Score>()
            .HasOne(s => s.Student)
            .WithMany(st => st.Scores)
            .HasForeignKey(s => s.StudentId)
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<Score>()
            .HasOne(s => s.Course)
            .WithMany(c => c.Scores)
            .HasForeignKey(s => s.CourseId)
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<RolePermission>()
            .HasOne(rp => rp.Role)
            .WithMany(r => r.RolePermissions)
            .HasForeignKey(rp => rp.RoleId)
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<RolePermission>()
            .HasOne(rp => rp.Permission)
            .WithMany(p => p.RolePermissions)
            .HasForeignKey(rp => rp.PermissionId)
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<UserRole>()
            .HasOne(ur => ur.User)
            .WithMany(u => u.UserRoles)
            .HasForeignKey(ur => ur.UserId)
            .OnDelete(DeleteBehavior.Cascade);

        modelBuilder.Entity<UserRole>()
            .HasOne(ur => ur.Role)
            .WithMany(r => r.UserRoles)
            .HasForeignKey(ur => ur.RoleId)
            .OnDelete(DeleteBehavior.Cascade);

        // 种子数据
        SeedData(modelBuilder);
    }

    private void SeedData(ModelBuilder modelBuilder)
    {
        // 种子角色数据
        modelBuilder.Entity<Role>().HasData(
            new Role { Id = 1, Name = "Admin", Description = "系统管理员", IsActive = true },
            new Role { Id = 2, Name = "Teacher", Description = "教师", IsActive = true },
            new Role { Id = 3, Name = "Student", Description = "学生", IsActive = true }
        );

        // 种子权限数据
        modelBuilder.Entity<Permission>().HasData(
            new Permission { Id = 1, Name = "ViewStudents", Description = "查看学生", Module = "Student" },
            new Permission { Id = 2, Name = "ManageStudents", Description = "管理学生", Module = "Student" },
            new Permission { Id = 3, Name = "ViewCourses", Description = "查看课程", Module = "Course" },
            new Permission { Id = 4, Name = "ManageCourses", Description = "管理课程", Module = "Course" },
            new Permission { Id = 5, Name = "ViewScores", Description = "查看成绩", Module = "Score" },
            new Permission { Id = 6, Name = "ManageScores", Description = "管理成绩", Module = "Score" },
            new Permission { Id = 7, Name = "ManageSystem", Description = "管理系统", Module = "System" }
        );

        // 种子角色权限关联数据
        modelBuilder.Entity<RolePermission>().HasData(
            // Admin角色拥有所有权限
            new RolePermission { RoleId = 1, PermissionId = 1 },
            new RolePermission { RoleId = 1, PermissionId = 2 },
            new RolePermission { RoleId = 1, PermissionId = 3 },
            new RolePermission { RoleId = 1, PermissionId = 4 },
            new RolePermission { RoleId = 1, PermissionId = 5 },
            new RolePermission { RoleId = 1, PermissionId = 6 },
            new RolePermission { RoleId = 1, PermissionId = 7 },
            // Teacher角色权限
            new RolePermission { RoleId = 2, PermissionId = 1 },
            new RolePermission { RoleId = 2, PermissionId = 3 },
            new RolePermission { RoleId = 2, PermissionId = 5 },
            new RolePermission { RoleId = 2, PermissionId = 6 },
            // Student角色权限
            new RolePermission { RoleId = 3, PermissionId = 1 },
            new RolePermission { RoleId = 3, PermissionId = 3 },
            new RolePermission { RoleId = 3, PermissionId = 5 }
        );

        // 种子用户数据（密码：Admin@123）
        modelBuilder.Entity<User>().HasData(
            new User 
            {
                Id = 1, 
                Username = "admin", 
                PasswordHash = "AQAAAAIAAYagAAAAEJkOvV8eJwHxY7y0w9OQoQ445fQOq1R6DnZ5L2H6H6Gq9X6N8Y7L5F3D4C1V2B0A==", 
                Email = "admin@example.com", 
                PhoneNumber = "13800138000", 
                FullName = "系统管理员", 
                IsActive = true, 
                CreatedAt = DateTime.Now, 
                LastLoginAt = DateTime.Now
            }
        );

        // 种子用户角色关联数据
        modelBuilder.Entity<UserRole>().HasData(
            new UserRole { UserId = 1, RoleId = 1 }
        );
    }
}